/*
 * @Author : restyhap
 * @Date   : 2023-01-17 18:04:33
 * @LastEditors: restyhap
 * @LastEditTime: 2023-02-12 09:57:32
 */
const vscode = require("vscode");
const rs = require("fs");
const path = require("path");
const { insertProcess, insertVueProcess } = require(path.join(__dirname, "insertLog.js"));
const { clearTab } = require(path.join(__dirname, "clearLog.js"));
const { tagProcess } = require('./changeTag.js');

/**
 *  获取支持的当前文件对象
 * @param {vscode.TextEditor} textEditor
 */
function getFileSuffix(textEditor) {
  // 获取当前文件后缀, 以确定是否对当前行进行操作。
  let currentFileName = textEditor.document.fileName;
  let currentSuffix = currentFileName.substring(currentFileName.lastIndexOf(".") + 1, currentFileName.length);
  return currentSuffix;
}

/**
 *  获取支持的当前文件对象
 * @param {vscode.TextEditor} textEditor
 */
function getSuportData(textEditor) {
  let suffixData = JSON.parse(rs.readFileSync(path.join(__dirname, "type.json"), { encoding: "utf8" })).data;
  let currentSuffix = getFileSuffix(textEditor);
  let data = {};
  for (let i = 0; i < suffixData.length; i++) {
    if (currentSuffix == suffixData[i].fileSuffix) {
      data = suffixData[i];
      break;
    }
  }
  return data;
}

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {
  let textEditor = vscode.window.activeTextEditor;
  // 当编辑器文本变化时，重置编辑器对象
  vscode.window.onDidChangeActiveTextEditor((editor) => (textEditor = editor));
  //

  let disposable1 = vscode.commands.registerCommand("resty-completion-log.add_completion_log", function () {
    textEditor.edit((editBuilder) => {
      // 1. 获取当前文件是否支持匹配的对象
      let data = getSuportData(textEditor);
      // 2.1 当文件不能处理时,后缀添加的内容
      let spaceStr = "  "; // 前置空格的添加
      // 2.2 获取当标所在行的位置
      // 2.2.1 获取光标所在行
      let cursorLine = textEditor.selection.active.line; // activeTextEditor.selection.active {c: 11, e: 39}
      // 2.2.2 获取光标所在行的位置
      let cursorPosition = textEditor.selection.start;
      let textObj = textEditor.document.lineAt(cursorLine); // 当前行内容对象
      let currentText = textObj.text; // 当前行内容
      let startPosition = new vscode.Position(cursorLine, 0);
      let endPosition = new vscode.Position(cursorLine, currentText.length);
      // 2. 判断当前的文件后缀是否支持
      if (data.fileSuffix == undefined) {
        let currentSuffix = getFileSuffix(textEditor);

        if (currentSuffix == "html") {
          let text = tagProcess(currentText);
          editBuilder.replace(new vscode.Range(startPosition, endPosition), "");
          editBuilder.insert(startPosition, text);
        } else {

          // 1.2 如果文件未做设置, 则最后添加一个缩进。 √
          editBuilder.insert(cursorPosition, spaceStr);

        }
      } else {
        // 1.2 如果文件已做设置, 则根据文件后缀进行处理。
        // 3 判断支持的文件是否是vue文件,根据判定结果, 实现逻辑处理。
        if (data.fileSuffix != "vue") {
          // 4、处理非vue逻辑
          if (currentText.includes(data.suffix)) {
            currentText = insertProcess(data, currentText);
            editBuilder.replace(new vscode.Range(startPosition, endPosition), "");
            editBuilder.insert(startPosition, currentText);
          } else {
            editBuilder.insert(cursorPosition, spaceStr);
          }
        } else {
          // 5、处理vue逻辑
          let text = insertVueProcess(textEditor, data);
          editBuilder.replace(new vscode.Range(startPosition, endPosition), "");
          editBuilder.insert(startPosition, text);
        }
      }
    });
  });

  let disposable2 = vscode.commands.registerCommand("resty-completion-log.clear_completion_log", function () {
    textEditor.edit((editorBuilder) => {
      let doc = textEditor.document;
      let array = clearTab(textEditor);
      array.forEach((item) => {
        let range = new vscode.Range(new vscode.Position(item, 0), new vscode.Position(item, doc.lineAt(item).text.length));
        editorBuilder.replace(range, "");
      })
      vscode.window.showWarningMessage("所有的输入语句已清除完成。一共清除了 " + array.length + " 条，感谢您的使用。");
    });
  });
  context.subscriptions.push(disposable1, disposable2);
}

// This method is called when your extension is deactivated
function deactivate() { }

module.exports = {
  activate,
  deactivate,
};
